//
//  FKCommodityBottomView.m
//  HappyTest
//
//  Created by Macbook Pro on 2019/2/15.
//  Copyright © 2019 Macbook Pro. All rights reserved.
//

#import "FKCommodityBottomView.h"
#import "FKCustomBtn.h"

@interface FKCommodityBottomView ()

@property (nonatomic, weak) FKCustomBtn *cartBtn;
@property (nonatomic, weak) UIButton *buyBtn;
@property (nonatomic, weak) UIButton *shareBtn;

@end

@implementation FKCommodityBottomView

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        [self setupSubviews];
    }
    return self;
}

- (void)setupSubviews
{
    UIView *separator = [UIView fk_lineWithFrame:CGRectMake(0, 0, fkScreenW, 0.5)];
    [self addSubview:separator];
    
    NSArray *titles = @[@"首页", @"客服", @"购物车"];
    NSArray *icons = @[@"goods_home", @"goods_service", @"goods_cart"];
    
    CGFloat whiteW = fk_adjustW(175);
    CGFloat itemBtnW = whiteW / 3.f;
    CGFloat itemH = fk_adjustW(50);
    for (NSInteger index = 0; index < titles.count; index++) {
        FKCustomBtn *btn = [FKCustomBtn fk_btnWithTarget:self action:@selector(itemBtnClicked:) font:fk_adjustFont(11) normalTitle:titles[index] normalTitleColor:[UIColor fk_colorWithHex:0x515151] selectedTitle:nil selectedTitleColor:nil normalImg:[UIImage imageNamed:icons[index]] selectedImg:nil bgImg:nil selectedBgImg:nil];
        [self addSubview:btn];
        btn.imagePosition = FKImagePositionUp;
        if (index == titles.count - 1) {
            self.cartBtn = btn;
        }
        
        CGFloat itemX = itemBtnW * index;
        btn.frame = CGRectMake(itemX, 0, itemBtnW, itemH);
    }
    
    UIButton *buyBtn = [FKCustomBtn fk_btnWithTarget:self action:@selector(buyBtnClicked:) font:fk_adjustFont(14) normalTitle:@"自买省" normalTitleColor:[UIColor whiteColor] selectedTitle:nil selectedTitleColor:nil normalImg:nil selectedImg:nil bgImg:nil selectedBgImg:nil];
    [self addSubview:buyBtn];
    self.buyBtn = buyBtn;
    buyBtn.titleLabel.numberOfLines = 2;
    buyBtn.titleLabel.textAlignment = NSTextAlignmentCenter;
    buyBtn.backgroundColor = [UIColor fk_colorWithHex:0x515151];
    buyBtn.frame = CGRectMake(whiteW, 0, fk_adjustW(100), itemH);
    
    UIButton *shareBtn = [FKCustomBtn fk_btnWithTarget:self action:@selector(shareBtnClicked:) font:fk_adjustFont(14) normalTitle:@"分享赚" normalTitleColor:[UIColor whiteColor] selectedTitle:nil selectedTitleColor:nil normalImg:nil selectedImg:nil bgImg:nil selectedBgImg:nil];
    [self addSubview:shareBtn];
    self.shareBtn = shareBtn;
    shareBtn.titleLabel.numberOfLines = 2;
    shareBtn.titleLabel.textAlignment = NSTextAlignmentCenter;
    shareBtn.backgroundColor = fkMainColor;
    shareBtn.frame = CGRectMake(buyBtn.fk_right, 0, fk_adjustW(100), itemH);
    
}

- (void)itemBtnClicked:(FKCustomBtn *)btn
{
    [FKLoginTool checkLogin:^{
        if ([self.delegate respondsToSelector:@selector(commodityBottomView:didClickedItemBtn:)]) {
            [self.delegate commodityBottomView:self didClickedItemBtn:btn];
        }
    }];
}

- (void)buyBtnClicked:(FKCustomBtn *)btn
{
    NSString *title = btn.titleLabel.text;
    if ([title containsString:@"购买"]) {
        if ([self.delegate respondsToSelector:@selector(commodityBottomView:didClickedBuyBtn:)]) {
            [self.delegate commodityBottomView:self didClickedBuyBtn:btn];
        }
    } else if ([title containsString:@"自买省"]) { // 会员的自买省就是加入购物车，然后去购物车付款，会员没有直接购买的概念
        if ([self.delegate respondsToSelector:@selector(commodityBottomView:didClickedAddCartBtn:)]) {
            [self.delegate commodityBottomView:self didClickedAddCartBtn:btn];
        }
    }
}

- (void)shareBtnClicked:(FKCustomBtn *)btn
{
    NSString *title = btn.titleLabel.text;
    if ([title containsString:@"分享赚"]) {
        if ([self.delegate respondsToSelector:@selector(commodityBottomView:didClickedShareBtn:)]) {
            [self.delegate commodityBottomView:self didClickedShareBtn:btn];
        }
    } else if ([title containsString:@"邀请"]) {
        if ([self.delegate respondsToSelector:@selector(commodityBottomView:didClickedInviteBtn:)]) {
            [self.delegate commodityBottomView:self didClickedInviteBtn:btn];
        }
    } else if ([title containsString:@"加入购物车"]) {
        if ([self.delegate respondsToSelector:@selector(commodityBottomView:didClickedAddCartBtn:)]) {
            [self.delegate commodityBottomView:self didClickedAddCartBtn:btn];
        }
    }
}

- (void)setCartNum:(NSString *)cartNum
{
    _cartNum = cartNum;
    
    self.cartBtn.number = cartNum;
}

- (void)updateTitle
{
    if (_special) {
        [self.buyBtn setTitle:@"购买" forState:UIControlStateNormal];
        [self.shareBtn setTitle:@"邀请" forState:UIControlStateNormal];
    } else {
        if (_member) {
            NSString *buyText = fkFORMAT(@"自买省\n￥%@", _cutPrice);
            [self.buyBtn setAttributedTitle:[buyText fk_addAttributeStyle:@"自买省" color:[UIColor whiteColor] font:fk_adjustFont(16)] forState:UIControlStateNormal];
            NSString *shareTitle = fkFORMAT(@"分享赚\n￥%@", _sharePrice);
            [self.shareBtn setAttributedTitle:[shareTitle fk_addAttributeStyle:@"自买省" color:[UIColor whiteColor] font:fk_adjustFont(16)] forState:UIControlStateNormal];
        } else {
            [self.buyBtn setTitle:@"立即购买" forState:UIControlStateNormal];
            [self.shareBtn setTitle:@"加入购物车" forState:UIControlStateNormal];
        }
    }
}

@end
